using System.Windows.Forms;
using Gl.SettingsManager;
using System;
using System.Configuration;
using System.Drawing;

namespace SettingsManagerTest
{

	public interface SchemaClass
	{
		[SettingProperty]
		Point Location {get; set;}

		[SettingProperty("Default form text")]
		string Text { get; set; }

		[SettingProperty]
		Color BackColor { get; set; }

		[SettingProperty]
		Size Size { get; set; }
	}

	[SettingsSchema(typeof(SchemaClass))]
	public partial class MainForm : Form
	{
		public MainForm()
		{
			StartPosition = FormStartPosition.Manual;
			InitializeComponent();
		}

		[SettingProperty]
		public string FormText
		{
			get { return textBox1.Text; }
			set { textBox1.Text = value; }
		}

		[SettingProperty]
		[Int32Validator(40, 48, DefaultValue = 40, ThrowExceptionOnNotValid = true)]
		public int IntFormProperty
		{
			get { return (int)numericUpDown1.Value; }
			set { numericUpDown1.Value = value; }
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			Settings.GetSettingsWithConfigurationFileProvider(ConfigurationUserLevel.PerUserRoamingAndLocal).Load(this);
		}

		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			Settings.GetSettingsWithConfigurationFileProvider(ConfigurationUserLevel.PerUserRoamingAndLocal).Save(this);
		}

		private void button2_Click(object sender, EventArgs e)
		{
			SubForm frm = new SubForm();
			frm.Show();
		}

		private void button3_Click(object sender, EventArgs e)
		{
			ColorDialog dialog = new ColorDialog();
			if (dialog.ShowDialog() == DialogResult.OK)
				this.BackColor = dialog.Color;
		}
	}
}